package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.Restrictions;
import com.kuanter.kuanterauto.entity.WearherInfo;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.JsonParser;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;

/**
 * 天气
 * @author kuanter
 *
 */
@Deprecated
public class TQActivity extends BaseActivity_NoBar {
	DataLoader loader;
	Button left_back;
	TextView top_middle;
	LinearLayout home_chose_City;
	
	static Restrictions restriction;  //限行信息
	ImageView forbid_img;
	RelativeLayout middle_weather_road; 
	RelativeLayout middle_weather_road1; 
	RelativeLayout middle_weather_road2; 
	
	LayoutInflater factory;
	ImageView forbid_img_1;
	ImageView forbid_img_2;
	
	TextView xxwh_1;
	TextView xxwh_2;
	
	TextView xczs_new;
	TextView xxwh_new;
	
	ImageView little_tq;
	ImageView little_tq_mt;
	ImageView little_tq_ht;
	
	public static String cityID;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_tq);
		factory = getLayoutInflater();
		initUI();
		
		xxwh_new = (TextView) findViewById(R.id.xxwh_new);
		xxwh_new.setVisibility(View.GONE);
		
		forbid_img = (ImageView) middle_weather_road.findViewById(R.id.forbid_img);
		forbid_img.setVisibility(View.GONE);
		
		forbid_img_1 = (ImageView) middle_weather_road1.findViewById(R.id.forbid_img);
		forbid_img_1.setVisibility(View.GONE);
		
		forbid_img_2 = (ImageView) middle_weather_road2.findViewById(R.id.forbid_img);
		forbid_img_2.setVisibility(View.GONE);
		
		
		xxwh_1 = (TextView) middle_weather_road1.findViewById(R.id.xxwh);
		xxwh_2 = (TextView) middle_weather_road2.findViewById(R.id.xxwh);
		
		if(restriction == null || cityID.equals("") || !cityID.equals(SharePrefenceUtil.getCityID(this))) {
			//获取限行信息
			DataLoader loader = new DataLoader(TQActivity.this);
			List<NameValuePair> params1 = new ArrayList<NameValuePair>();
			NameValuePair cityID = new BasicNameValuePair("cityCode",SharePrefenceUtil.getCityID(TQActivity.this));
			params1.add(cityID);
			//TODO
			loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.findRestriction, params1, restri_handler, "", false);
		} else {
			
//			forbid_img.setVisibility(View.VISIBLE);
//			lin_xxwh.setVisibility(View.VISIBLE);
//			xxwh.setText(restriction.getdRNumber());
			
			if(restriction.getdRRegion() != null && !restriction.getdRRegion().equals("")) {
				forbid_img.setVisibility(View.VISIBLE);
			} else {
				forbid_img.setVisibility(View.GONE);
			}
			if(restriction.getdRNumber() != null && !restriction.getdRNumber().equals("")) {
				
				xxwh_new.setVisibility(View.VISIBLE);
				xxwh_new.setText("限行尾号:" + restriction.getdRNumber().replace("[", "(").replace("]", ")"));
			} else {
				
				xxwh_new.setVisibility(View.GONE);
			}
		}
		
		
		
		if(wea_info == null  || cityID.equals("") || !cityID.equals(SharePrefenceUtil.getCityID(this))) {
			String cityforweatherID = SharePrefenceUtil.getCityIDForWeather(TQActivity.this);
			DataLoader loadWeather = new DataLoader(this);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			NameValuePair nvp = new BasicNameValuePair("cityId",cityforweatherID);
			params.add(nvp);
			loadWeather.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDCITYWEATHER, params, wea_handler, "", false);
//			new Thread(new WeaRun("http://m.weather.com.cn/data/" + cityforweatherID + ".html",arg_week)).start();
//			new Thread(new WeaRun("http://www.weather.com.cn/data/sk/" + cityforweatherID + ".html",arg_day)).start();
		}
		
		initWeather();
	}

	private void initUI() {
		middle_weather_road = (RelativeLayout) findViewById(R.id.middle_weather_road);
		middle_weather_road1 = (RelativeLayout) findViewById(R.id.middle_weather_road_1);
		middle_weather_road2 = (RelativeLayout) findViewById(R.id.middle_weather_road_2);
		
		little_tq = (ImageView) findViewById(R.id.little_tq);
		little_tq_mt = (ImageView) findViewById(R.id.little_tq_mt);
		little_tq_ht = (ImageView) findViewById(R.id.little_tq_ht);
		
		left_back = (Button) findViewById(R.id.left_back);
		left_back.setVisibility(View.GONE);
		
		top_middle = (TextView) findViewById(R.id.top_middle);
		top_middle.setText("出行信息");
		
		home_chose_City = (LinearLayout) findViewById(R.id.home_chose_City);
		home_chose_City.setVisibility(View.GONE);
		
	}
	
	
	TextView weather_temp;
	TextView weather_tq;
	
	LinearLayout lin_cx_ad;
	ImageView deleteit;
	TextView first_ad;
	TextView next_ad;
	
	View xxxx;  //限行信息;
	TextView rule;
	TextView region;
	TextView res_time;
	
	Dialog dialog;  //限行消息
	TextView weather_temp_1;
	TextView weather_tq_1;
	
	TextView weather_temp_2;
	TextView weather_tq_2;
	
	private void initWeather() {
		xczs_new = (TextView) findViewById(R.id.xczs_new);
		
		weather_temp = (TextView) middle_weather_road.findViewById(R.id.weather_temp);
		weather_tq = (TextView) middle_weather_road.findViewById(R.id.weather_tq);
		
		weather_temp_1 = (TextView) middle_weather_road1.findViewById(R.id.weather_temp);
		weather_tq_1 = (TextView) middle_weather_road1.findViewById(R.id.weather_tq);
		
		weather_temp_2 = (TextView) middle_weather_road2.findViewById(R.id.weather_temp);
		weather_tq_2 = (TextView) middle_weather_road2.findViewById(R.id.weather_tq);
		
		if(wea_info != null) {
			weather_temp.setText(wea_info.getTemp().split(",")[0] + "°");
			weather_tq.setText(wea_info.getTq_info().split(",")[0]);
//			middle_weather_road.setBackgroundResource(wea_info.getImgRes());
			getImg(wea_info.getTq_info().split(",")[0],middle_weather_road);
			getImg(wea_info.getTq_info().split(",")[0], little_tq);
			
			xczs_new.setText(wea_info.getXc_number() + "洗车");
			
			weather_temp_1.setText(wea_info.getTemp().split(",")[1] + "");
			weather_tq_1.setText(wea_info.getTq_info().split(",")[1]);
			getImg(wea_info.getTq_info().split(",")[1],middle_weather_road1);
			getImg(wea_info.getTq_info().split(",")[1], little_tq_mt);
			
			weather_temp_2.setText(wea_info.getTemp().split(",")[2] + "");
			weather_tq_2.setText(wea_info.getTq_info().split(",")[2]);
			getImg(wea_info.getTq_info().split(",")[2],middle_weather_road2);
			getImg(wea_info.getTq_info().split(",")[2], little_tq_ht);
			
			if(wea_info.getXc_number().equals(WearherInfo.bsy) || wea_info.getXc_number().equals(WearherInfo.jbsy)) {
				xczs_new.setTextColor(Color.WHITE);
			} else {
				xczs_new.setTextColor(Color.parseColor("#DAFB4C"));
			}
		}
		//TODO
		forbid_img.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				xxxx = factory.inflate(R.layout.restriction, null);
				rule = (TextView) xxxx.findViewById(R.id.rule);
				region = (TextView) xxxx.findViewById(R.id.region);
				res_time = (TextView) xxxx.findViewById(R.id.res_time);
				dialog = new Dialog(TQActivity.this,R.style.selectorDialog);
				dialog.setTitle("限行信息");
				dialog.setContentView(xxxx);
				if(restriction == null) {
					showProgress("正在获取限行信息...");
					DataLoader loader = new DataLoader(TQActivity.this);
					List<NameValuePair> params1 = new ArrayList<NameValuePair>();
					NameValuePair cityID = new BasicNameValuePair("cityCode",SharePrefenceUtil.getCityID(TQActivity.this));
					params1.add(cityID);
					//TODO
					loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.findRestriction, params1, restri_handler, "", false);
				} else {
					rule.setText(restriction.getdRRule() + "");
					region.setText(restriction.getdRRegion() + "");
//					res_time.setText(restriction.getdRTime() + "");
					dialog.show();
				}
				
			}
		});
		
	}
	
	@Override
	protected void onResume() {
		if(restriction == null || cityID.equals("") || !cityID.equals(SharePrefenceUtil.getCityID(this))) {
			//获取限行信息
			DataLoader loader = new DataLoader(TQActivity.this);
			List<NameValuePair> params1 = new ArrayList<NameValuePair>();
			NameValuePair cityID = new BasicNameValuePair("cityCode",SharePrefenceUtil.getCityID(TQActivity.this));
			params1.add(cityID);
			//TODO
			loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.findRestriction, params1, restri_handler, "", false);
		} else {
			
//			forbid_img.setVisibility(View.VISIBLE);
//			lin_xxwh.setVisibility(View.VISIBLE);
//			xxwh.setText(restriction.getdRNumber());
			
			if(restriction.getdRRegion() != null && !restriction.getdRRegion().equals("")) {
				forbid_img.setVisibility(View.VISIBLE);
			} else {
				forbid_img.setVisibility(View.GONE);
			}
			if(restriction.getdRNumber() != null && !restriction.getdRNumber().equals("")) {
				
				xxwh_new.setVisibility(View.VISIBLE);
				xxwh_new.setText("限行尾号:" + restriction.getdRNumber().replace("[", "(").replace("]", ")"));
				
			} else {
				
				xxwh_new.setVisibility(View.GONE);
			}
		}
		
		
		
		if(wea_info == null  || cityID.equals("") || !cityID.equals(SharePrefenceUtil.getCityID(this))) {
			String cityforweatherID = SharePrefenceUtil.getCityIDForWeather(TQActivity.this);
			DataLoader loadWeather = new DataLoader(this);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			NameValuePair nvp = new BasicNameValuePair("cityId",cityforweatherID);
			params.add(nvp);
			loadWeather.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDCITYWEATHER, params, wea_handler, "", false);
//			new Thread(new WeaRun("http://m.weather.com.cn/data/" + cityforweatherID + ".html",arg_week)).start();
//			new Thread(new WeaRun("http://www.weather.com.cn/data/sk/" + cityforweatherID + ".html",arg_day)).start();
		}
		
		initWeather();
		super.onResume();
	}

	public void getImg(String s,RelativeLayout iv) {
		//设置图片
		int position = s.lastIndexOf("转");
		if(position != -1) {
			s = s.substring(0, position);
		}
		if(s.contains("沙")) {
			iv.setBackgroundResource(R.drawable.wea_li);
		} else if(s.contains("霾")) {
			iv.setBackgroundResource(R.drawable.wea_li);
		} else if(s.contains("雷")) {
			iv.setBackgroundResource(R.drawable.wea_lei);
		} else if(s.contains("雾")) {
			iv.setBackgroundResource(R.drawable.wea_wu);
		} else if(s.contains("晴")) {
			iv.setBackgroundResource(R.drawable.wea_qtian);
		} else if(s.contains("云")) {
			iv.setBackgroundResource(R.drawable.wea_duoyun);
		} else if(s.contains("阴")) {
			iv.setBackgroundResource(R.drawable.wea_yin);
		} else if(s.contains("雪")) {
			iv.setBackgroundResource(R.drawable.wea_xue);
		} else if(s.contains("雨")) {
			iv.setBackgroundResource(R.drawable.wea_yu);
		}
	}
	
	public void getImg(String s,ImageView iv) {
		//设置图片
		int position = s.lastIndexOf("转");
		if(position != -1) {
			s = s.substring(0, position);
		}
		if(s.contains("沙")) {
			iv.setImageResource(R.drawable.fog_day);
		} else if(s.contains("霾")) {
			iv.setImageResource(R.drawable.fog_day);
		} else if(s.contains("雷")) {
			iv.setImageResource(R.drawable.storm);
		} else if(s.contains("雾")) {
			iv.setImageResource(R.drawable.fog_day);
		} else if(s.contains("晴")) {
			iv.setImageResource(R.drawable.sun);
		} else if(s.contains("云")) {
			iv.setImageResource(R.drawable.clouds);
		} else if(s.contains("阴")) {
			iv.setImageResource(R.drawable.fog_day);
		} else if(s.contains("雪")) {
			iv.setImageResource(R.drawable.snow);
		} else if(s.contains("雨")) {
			iv.setImageResource(R.drawable.rain);
		}
	}

	/*
	 * 获取限行信息的hanlder
	 */
	Handler restri_handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == HttpTask.RESULT_OK) {
				String json = (String) msg.obj;
				Log.i("bbb", json + "限行");
				restriction = JsonParser.getRestri(json);
				if(restriction != null) {
//					if(dialog != null) {
//						res_time.setText(restriction.getdRTime() + "");
//						region.setText(restriction.getdRRegion() + "");
//						rule.setText(restriction.getdRRule() + "");
//						dialog.show();
//					}
					if(restriction.getdRRegion() != null && !restriction.getdRRegion().equals("")) {
						forbid_img.setVisibility(View.VISIBLE);
					} else {
						forbid_img.setVisibility(View.GONE);
					}
					
					if(restriction.getdRNumber() != null && !restriction.getdRNumber().equals("")) {
						
						xxwh_new.setVisibility(View.VISIBLE);
						xxwh_new.setText("限行尾号:" + restriction.getdRNumber().replace("[", "(").replace("]", ")"));
						
					} else {
						
						xxwh_new.setVisibility(View.GONE);
					}
					
				} else {
//					showToast("暂无限行信息");
					forbid_img.setVisibility(View.GONE);
				}
			}
			super.handleMessage(msg);
		}
		
	};
	
	static WearherInfo wea_info = null;
//	String wea_temp;
	Handler wea_handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == HttpTask.RESULT_OK) {
				//TODO
				cityID = SharePrefenceUtil.getCityID(TQActivity.this);
				
				String json = (String) msg.obj;
				wea_info = JsonParser.getWeatherInfo(json);
				if (wea_info != null) {
					
					// ---
					if (wea_info.getXc_number() != null) {
						xczs_new.setText(wea_info.getXc_number() + "洗车");
						if (wea_info.getXc_number().equals(WearherInfo.bsy)
								|| wea_info.getXc_number().equals(
										WearherInfo.jbsy)) {
							xczs_new.setTextColor(Color.WHITE);
						} else {
							xczs_new.setTextColor(Color.parseColor("#DAFB4C"));
						}
					}
					// ----
//					middle_weather_road.setBackgroundResource(wea_info
//							.getImgRes());
					getImg(wea_info.getTq_info().split(",")[0],middle_weather_road);
					
					weather_temp.setText(wea_info.getTemp().split(",")[0] + "°");
					weather_tq.setText(wea_info.getTq_info().split(",")[0]);
					getImg(wea_info.getTq_info().split(",")[0], little_tq);
					
					weather_temp_1.setText(wea_info.getTemp().split(",")[1] + "");
					weather_tq_1.setText(wea_info.getTq_info().split(",")[1]);
					getImg(wea_info.getTq_info().split(",")[1],middle_weather_road1);
					getImg(wea_info.getTq_info().split(",")[1], little_tq_mt);
					
					weather_temp_2.setText(wea_info.getTemp().split(",")[2] + "");
					weather_tq_2.setText(wea_info.getTq_info().split(",")[2]);
					getImg(wea_info.getTq_info().split(",")[2],middle_weather_road2);
					getImg(wea_info.getTq_info().split(",")[2], little_tq_ht);
				}
			}
			super.handleMessage(msg);
		}

	};
	
}
